<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
define("PRIMARY_KEY","kdcust");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_transaction_memo_thirteenth.php";
$FileTemplate  = "ais_transaction_memo_thirteenth_memolist.html";
$PageSize      = 10 ;
$TableName     = "ais_hutang_remain";
$HTMLGridList  = "CustomerGrid";
//************ FORM ACTION **************
    if (GetParam("Action","")=="Delete") {
        DeleteRecord ("txtCheck",$PageSize);
    }

$FCode = GetParam("memorialCodeSearch","");
$FDebtur = GetParam("debtur","");
$FDate = GetParam("transDate","");

//Initiate Database Table For Search;
//******* DATABASE FORM QUERY ***********
$DBMysql = $DBConnection->GetConnection1();

GridList (GetParam("transDate",""),GetParam("office",""),GetParam("curr",""),GetParam("debtur",""));
/*
$SQL      = "
                 SELECT $TableName.*, ais_memorial.princ, ais_memorial.vessel, ais_memorial.voyage, ais_memorial.port, ais_memorial.kdact
                 ,ais_hutang.nobkta,ais_hutang.rate,ais_hutang.skada, ais_memorial.dk as dk, (ifnull(jumlahcurr,0)+ifnull(jumlah30,0)+ifnull(jumlah60,0)+ifnull(jumlah60g,0)) as total_hutang
                 FROM $TableName
                 INNER JOIN ais_memorial ON $TableName.nobkt=ais_memorial.nobkt AND ais_memorial.skada=$TableName.skada  AND ais_memorial.noref=$TableName.noref AND ais_memorial.uraian=$TableName.uraian
                 INNER JOIN ais_hutang ON $TableName.nobkt=ais_hutang.nobkt AND ais_hutang.skada=$TableName.skada  AND ais_hutang.noref=$TableName.noref AND ais_hutang.uraian=$TableName.uraian
                 WHERE ais_hutang.debtur='".GetParam("debtur","")."' AND $TableName.nobkt=ais_hutang.nobkt AND (ifnull(jumlahcurr,0)+ifnull(jumlah30,0)+ifnull(jumlah60,0)+ifnull(jumlah60g,0)) > 0
                ";
*/
$SQL      = "
                 SELECT $TableName.*, ais_memorial.princ, ais_memorial.vessel, ais_memorial.voyage, ais_memorial.port, ais_memorial.kdact
                 ,ais_hutang.nobkta,ais_hutang.rate,ais_hutang.skada, ais_memorial.dk as dk, (ifnull(jumlahcurr,0)+ifnull(jumlah30,0)+ifnull(jumlah60,0)+ifnull(jumlah60g,0)) as total_hutang
                 FROM $TableName
                 INNER JOIN ais_memorial ON ais_hutang_remain.nobkt = ais_memorial.nobkt
                 AND ais_memorial.urut =999
                 INNER JOIN ais_hutang ON $TableName.nobkt=ais_hutang.nobkt AND ais_hutang.skada=$TableName.skada  AND ais_hutang.noref=$TableName.noref AND ais_hutang.uraian=$TableName.uraian
                 WHERE ais_hutang.debtur='".GetParam("debtur","")."' AND $TableName.nobkt=ais_hutang.nobkt AND (ifnull(jumlahcurr,0)+ifnull(jumlah30,0)+ifnull(jumlah60,0)+ifnull(jumlah60g,0)) > 0
                ";

                //print $SQL;






$SQLCount = "SELECT count(*)
                   FROM $TableName
                 INNER JOIN ais_memorial ON ais_hutang_remain.nobkt = ais_memorial.nobkt
                 AND ais_memorial.urut =999
                 INNER JOIN ais_hutang ON $TableName.nobkt=ais_hutang.nobkt AND ais_hutang.skada=$TableName.skada  AND ais_hutang.noref=$TableName.noref AND ais_hutang.uraian=$TableName.uraian
                   WHERE ais_hutang.debtur='".GetParam("debtur","")."' AND $TableName.nobkt=ais_hutang.nobkt (ifnull(jumlahcurr,0)+ifnull(jumlah30,0)+ifnull(jumlah60,0)+ifnull(jumlah60g,0)) > 0
                   ";

//******* SHOW FORM CONTENT *********
//$SQL .= "GROUP BY $TableName.nobkt ";
//$SQLCount .= " GROUP BY $TableName.nobkt ";


$PageNum = 1;

$RecCnt  = $DBConnection->GetResSQL($SQLCount,$DBMysql);

//print $SQL;

if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

//Default value
if (!isset($_GET)) $_GET=&$HTTP_GET_VARS ;
if (isset($_GET['PageNum'])) {
  $PageNum = $_GET['PageNum'] ;
} else {
    $PageNum = 1 ;
}

//Default value
if (isset($_GET['RecCnt'])) {
  $RecCnt = intval($_GET['RecCnt']) ;
} else {
    $RecCnt = -1 ;
}

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);

$RecCnt = $TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
//print $SQL;
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ;
$TBS->Show();

function FlushCustomer ($TableName, $tmp) {
    global $DBConnection,$DBGeneral;

    if ($tmp) $TEMPORARY = "TEMPORARY";

    $SQL = "DROP TABLE IF EXISTS `$TableName`;";
    $DBConnection->dbc->query($SQL);

    $SQL= "CREATE $TEMPORARY TABLE `$TableName` (
              `kodecustomer` varchar(20) NOT NULL default '',
              `nama` varchar(50) default NULL,
              `cr_term` int(4) default NULL
              )";

    $DBConnection->dbc->query($SQL);
  $SQL = "INSERT INTO `$TableName` SELECT cucode,cuname,top FROM tbldebtur GROUP BY CUDEBTUR ";
  $DBConnection->dbc->query($SQL);
    return true;
}

function GridList ($transDate,$office,$curr,$debtur) {
            global $DBConnection;
            //$TEMP = "TEMPORARY";

            FlushCustomer ("ais_customer", 0);
            //$current_date = date("Y-m-d",mktime (0,0,0,GetParam("m",""),1,GetParam("y","")));

            list($day,$month,$year) = explode("/",$transDate);
            $current_date = "$year-$month-$day";

            $SQL = "DROP TABLE IF EXISTS `ais_hutang_terhutang`";
            $DBConnection->dbc->query($SQL);

            $SQL = "
                    CREATE $TEMP TABLE `ais_hutang_terhutang` (
                      `ktrasl` varchar(5) default NULL,
                      `tglbkt` date,
                      `nobkt` varchar(16) default NULL,
             `nobkta` varchar(16) default NULL,
                      `debtur` varchar(16) default NULL,
                      `debnam` varchar(32) default NULL,
                      `uraian` text default NULL,
                      `curr` varchar(4) default NULL,
                      `jumlah` varchar(32) default NULL,
                      `cr_term` tinyint(4)default NULL,
                      `aging` varchar(4)default NULL,
                      `noref` varchar(32)default NULL,
                      `skada` varchar(32)default NULL
                    ) TYPE=MyISAM;
                   ";
            $DBConnection->dbc->query($SQL);

            $SQL = "
                    INSERT INTO ais_hutang_terhutang
                    SELECT KTRASL, TGLBKT, NOBKT,NOBKTA, DEBTUR, DEBNAM, URAIAN, CURR, JUMLAH,  IFNULL(CR_TERM,0) AS TOP,
                    IF('$current_date'<=ADDDATE( tglbkt,INTERVAL ais_customer.cr_term DAY ),'0',
                    IF('$current_date'<=ADDDATE( tglbkt,INTERVAL 30+ais_customer.cr_term DAY ),'30',
                    IF('$current_date'<=ADDDATE( tglbkt,INTERVAL 60+ais_customer.cr_term DAY ),'60','60g'))), noref, skada
                    FROM ais_hutang
                    LEFT JOIN ais_customer ON ais_hutang.DEBTUR=ais_customer.kodecustomer
                    WHERE srt='1' AND ais_hutang.tglbkt<='$current_date' ";

                    if ($office)   $SQL .= " AND ais_hutang.ktrasl = '".$office."' ";
                    if ($curr)   $SQL .= " AND ais_hutang.curr = '".$curr."' ";
                    if ($debtur) $SQL .=" AND ais_customer.kodecustomer  = '".$debtur."' ";
                                        $SQL .= " order by nobkt";

                        //print $SQL;die();

            $DBConnection->dbc->query($SQL);

            $SQL = "DROP TABLE IF EXISTS `ais_hutang_terbayar`";
            $DBConnection->dbc->query($SQL);

            $SQL = "
                    CREATE $TEMP TABLE `ais_hutang_terbayar` (
                      `nobkt` varchar(16) default NULL,
                      `nobkta` varchar(16) default NULL,
                                          `bayar` varchar(32) default NULL
                    ) TYPE=MyISAM;
                   ";
            $DBConnection->dbc->query($SQL);

            $SQL = "
                    INSERT INTO ais_hutang_terbayar
                    SELECT NOBKT, NOBKTA, SUM(JUMLAH)
                    FROM ais_hutang
                                        LEFT JOIN ais_customer ON ais_hutang.DEBTUR=ais_customer.kodecustomer
                    WHERE srt='2' AND ais_hutang.tglbkt<='$current_date' ";
                                        if ($office)   $SQL .= " AND ais_hutang.ktrasl = '".$office."' ";
                                        if ($curr)   $SQL .= " AND ais_hutang.curr = '".$curr."' ";
                                        if ($debtur) $SQL .=" AND ais_customer.kodecustomer  = '".$debtur."' ";

                        $SQL .= " group by nobkta order by nobkta";
                        //print $SQL;die();
            $DBConnection->dbc->query($SQL);


            $SQL = "DROP TABLE IF EXISTS `ais_hutang_remain`";
            $DBConnection->dbc->query($SQL);

            $SQL = "
                    CREATE $TEMP TABLE `ais_hutang_remain` (
                      `ktrasl` varchar(5) default NULL,
                      `tglbkt` date,
                      `nobkt` varchar(16) default NULL,
                      `debtur` varchar(16) default NULL,
                      `debnam` varchar(32) default NULL,
                      `uraian` text default NULL,
                      `curr` varchar(4) default NULL,
             `cr_term` tinyint(4)default NULL,
                      `jumlahcurr` decimal(15,2)default NULL,
                      `jumlah30` decimal(15,2)default NULL,
                      `jumlah60` decimal(15,2)default NULL,
                      `jumlah60g` decimal(15,2)default NULL,
                      `noref` varchar(32)default NULL,
                      `skada` varchar(32)default NULL
                    ) TYPE=MyISAM;
                   ";
            $DBConnection->dbc->query($SQL);


                        $SQL = "
                                        INSERT INTO ais_hutang_remain
                                        SELECT
                                        ais_hutang_terhutang.ktrasl,
                                        ais_hutang_terhutang.tglbkt,
                                        ais_hutang_terhutang.nobkt,
                                        ais_hutang_terhutang.debtur,
                                        ais_hutang_terhutang.debnam,
                                        ais_hutang_terhutang.uraian,
                                        ais_hutang_terhutang.curr,
                                        ifnull(ais_hutang_terhutang.cr_term, 0) AS top,
                                        if(ais_hutang_terhutang.aging='0', ais_hutang_terhutang.jumlah-ifnull(ais_hutang_terbayar.bayar,0) ,0) as jumlahcurr,
                                        if(ais_hutang_terhutang.aging='30', ais_hutang_terhutang.jumlah-ifnull(ais_hutang_terbayar.bayar,0) ,0) as jumlah30,
                                        if(ais_hutang_terhutang.aging='60', ais_hutang_terhutang.jumlah-ifnull(ais_hutang_terbayar.bayar,0) ,0) as jumlah60,
                                        if(ais_hutang_terhutang.aging='60g', ais_hutang_terhutang.jumlah-ifnull(ais_hutang_terbayar.bayar,0) ,0) as jumlah90,
                                        noref,skada
                                        FROM
                                        ais_hutang_terhutang
                                        LEFT JOIN ais_hutang_terbayar ON (ais_hutang_terhutang.nobkt = ais_hutang_terbayar.nobkta)
                                        WHERE
                                          (ifnull(ais_hutang_terbayar.nobkt, '') = '') OR (ais_hutang_terhutang.jumlah-ais_hutang_terbayar.bayar) <> 0
                                        ORDER BY debtur,nobkt
                        ";
            $DBConnection->dbc->query($SQL);

}

?>
